﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
using System.Net;

namespace VoteUphold
{
    /// <summary>
    /// 判断网络连接
    /// </summary>
    public class IsConnect
    {

        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

        private Ping ping = new Ping();
        /// <summary>
        /// //判断网络是否连接
        /// </summary>
        /// <returns></returns>
        private static bool GetConnectedState()
        {
            int I = 0;
            bool state = InternetGetConnectedState(out I, 0);
            return state;
        }
        /// <summary>
        /// 是否能接入互联网
        /// </summary>
        /// <returns>获取 Internet 控制消息协议 (ICMP) 回送答复消息中收到的数据缓冲区。</returns>
        public byte[] IsConnected()
        {
            PingReply pr=null;
            try
            {
                if (GetConnectedState())
                {
                    pr = ping.Send("www.baidu.com");
                    return pr.Buffer;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return null;
            
        }
    }
}
